<?php
/**
 编辑分类与数据库关联
 */
//获取修改后的分类数据
$categoryId = $_POST['category_id'];
$categoryName = $_POST['category_name'];
$categoryDesc = $_POST['category_desc'];

//检测分类Id
if (empty($categoryId)){
    echo "分类id错误";
    exit();
}
//分类名称2-40个字
if (!(mb_strlen($categoryName) >= 2 && mb_strlen($categoryName) <= 40)){
    echo "分类名称2-40个字<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}
//分类描述10-400个字
if (!(mb_strlen($categoryDesc) >= 10 && mb_strlen($categoryDesc) <= 400)){
    echo "分类描述10-400个字<br/>";
    echo "<a href='javascript:history.go(-1)'>返回上一页</a>";
    exit();
}

//设置时区,获取时间
date_default_timezone_set("PRC");
$updateTime = time();

//连接到数据库
$dsn = "mysql:host=127.0.0.1;dbname=blog";
$db = new PDO($dsn, "root", "123456");

//更改默认的utf8为utf8mb4以防中文乱码
$db->exec("set names utf8mb4");

//php语句mysql修改数据
$sql = "update category 
set category_name = '$categoryName', category_desc = '$categoryDesc', update_time = '$updateTime' 
where category_id = '$categoryId'";
$result = $db->exec($sql);
if ($result){
    echo "编辑分类成功。<a href='category_list.php'>返回分类列表页</a>>";
    exit();
}else{
    echo "编辑分类失败。错误信息：".$db->errorInfo()[2].",请联系管理员123 ";
}